package com.stockz.downloader.breadth.bse;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.springframework.util.StringUtils;

import com.stockz.equity.model.Bar;
import com.stockz.equity.model.Scrip;

public class BseBarBredthCSVParser {

	public static List<Bar> parseCsv(String s){
		List<Bar> barList = new ArrayList<Bar>();
		if(StringUtils.hasText(s)){
			StringTokenizer t = new StringTokenizer(s,"\n",false);
			while(t.hasMoreTokens()){
				Bar b = parseString(t.nextToken());
				if(b != null){
					barList.add(b);					
				}
			}
		}
		return barList;
	}
	
	public static Bar parseString(String str){
		Bar bar = new Bar();
		Scrip scrip = new Scrip();
		if(StringUtils.hasText(str)){
			StringTokenizer t = new StringTokenizer(str,",",false);
			try{
				scrip.setScripCode(t.nextToken());
				scrip.setScripName(t.nextToken());//bar.setName(t.nextToken());
				t.nextToken();//bar.setGroup(t.nextToken());
				t.nextToken();//bar.setType(t.nextToken());
				bar.setScrip(scrip);
				bar.setScripCode(scrip.getScripCode());
				bar.setOpen(Double.parseDouble(t.nextToken()));
				bar.setHigh(Double.parseDouble(t.nextToken()));
				bar.setLow(Double.parseDouble(t.nextToken()));
				bar.setClose(Double.parseDouble(t.nextToken()));
				bar.setLast(Double.parseDouble(t.nextToken()));
				bar.setPreviousClose(Double.parseDouble(t.nextToken()));
				bar.setTrades(Double.parseDouble(t.nextToken()));
				bar.setVolume(Double.parseDouble(t.nextToken()));
				bar.setTurnover(Double.parseDouble(t.nextToken()));
			}catch(Exception e){
				bar = null;
			}
		}
		
		return bar;
	}
	
}
